package edu.cmu.cs.webapp.ioms.ui.wizards.order;

import java.util.ArrayList;
import java.util.List;

import org.eclipse.jface.dialogs.IDialogConstants;
import org.eclipse.jface.viewers.IStructuredSelection;
import org.eclipse.jface.viewers.TableViewer;
import org.eclipse.jface.wizard.WizardPage;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.events.SelectionListener;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Table;
import org.eclipse.swt.widgets.TableColumn;

import edu.cmu.cs.webapp.ioms.models.LineItem;
import edu.cmu.cs.webapp.ioms.ui.dialogs.order.OrderItemDialog;
import edu.cmu.cs.webapp.ioms.ui.providers.order.ItemsTableViewerContentProvider;
import edu.cmu.cs.webapp.ioms.ui.providers.order.ItemsTableViewerLabelProvider;
import edu.cmu.cs.webapp.ioms.utils.SWTUtils;

/**
 * 
 * @author Christian
 */
public class OrderItemWizardPage extends WizardPage {

	private Button _addBtn;
	private Button _editBtn;
	private Button _delBtn;
	private Button _clrBtn;

	private TableViewer itemsViewer;
	private Table table;
	private List<LineItem> items;

	public OrderItemWizardPage(String pageName) {
		super(pageName);
		setTitle("Order Items");
		setDescription("Add line items to this order");
		items = new ArrayList<LineItem>();

	}

	public void createControl(Composite parent) {
		final Composite container = new Composite(parent, SWT.NULL);
		GridLayout layout = new GridLayout();
		container.setLayout(layout);
		layout.numColumns = 2;
		// layout.verticalSpacing = 9;

		createTableViewer(container);

		createButtonSection(container);

		setControl(container);

	}

	private void createTableViewer(Composite container) {

		/*
		 * Create table
		 */
		GridData gridData = new GridData();
		gridData.grabExcessHorizontalSpace = true;
		gridData.grabExcessVerticalSpace = true;
		gridData.horizontalAlignment = GridData.FILL;
		gridData.verticalAlignment = GridData.FILL;

		table = new Table(container, SWT.SINGLE | SWT.BORDER | SWT.H_SCROLL
				| SWT.V_SCROLL | SWT.FULL_SELECTION);

		table.setLayoutData(gridData);

		table.setHeaderVisible(true);
		table.setLinesVisible(false);

		// Product
		TableColumn productColumn = new TableColumn(table, SWT.NONE);
		productColumn.setText("Product"); //$NON-NLS-1$
		productColumn.setWidth(115);

		// Package Spec
		TableColumn pacColumn = new TableColumn(table, SWT.NONE);
		pacColumn.setText("Package Specification"); //$NON-NLS-1$
		pacColumn.setWidth(140);

		// order quantity
		TableColumn quantityColumn = new TableColumn(table, SWT.NONE);
		quantityColumn.setText("Order Quantity"); //$NON-NLS-1$
		quantityColumn.setWidth(100);

		/*
		 * Create viewer
		 */
		itemsViewer = new TableViewer(table);

		itemsViewer.setUseHashlookup(true);
		itemsViewer.setLabelProvider(new ItemsTableViewerLabelProvider());
		itemsViewer.setContentProvider(new ItemsTableViewerContentProvider());
		itemsViewer.setInput(items.toArray(new LineItem[0]));
	}

	private void createButtonSection(Composite container) {
		// button composite
		Composite btnComp = new Composite(container, SWT.VERTICAL | SWT.BORDER);
		GridLayout gl = new GridLayout();
		btnComp.setLayout(gl);
		GridData gd = new GridData(GridData.FILL_BOTH);
		gd.verticalAlignment = GridData.BEGINNING;
		btnComp.setLayoutData(gd);

		this._addBtn = SWTUtils.createPushButton(btnComp, "Add");
		this._addBtn.addSelectionListener(new SelectionListener() {

			@Override
			public void widgetSelected(SelectionEvent e) {
				OrderItemDialog dlg = new OrderItemDialog(getShell());
				if (dlg.open() == IDialogConstants.OK_ID) {
					items.add(dlg.getModel());
					itemsViewer.setInput(items.toArray(new LineItem[0]));
				}
			}

			@Override
			public void widgetDefaultSelected(SelectionEvent e) {
				// TODO Auto-generated method stub

			}
		});

		this._editBtn = SWTUtils.createPushButton(btnComp, "Edit");
		this._editBtn.addSelectionListener(new SelectionListener() {

			@Override
			public void widgetSelected(SelectionEvent e) {
				Object sel = ((IStructuredSelection) itemsViewer.getSelection())
						.getFirstElement();
				if (sel != null && sel instanceof LineItem) {
					LineItem item = (LineItem) sel;

					OrderItemDialog dlg = new OrderItemDialog(getShell(), item);
					if (dlg.open() == IDialogConstants.OK_ID) {
						items.remove(item);
						items.add(dlg.getModel());
						itemsViewer.setInput(items.toArray(new LineItem[0]));
					}
				}
			}

			@Override
			public void widgetDefaultSelected(SelectionEvent e) {
				// TODO Auto-generated method stub

			}
		});

		this._delBtn = SWTUtils.createPushButton(btnComp, "Delete");
		this._delBtn.addSelectionListener(new SelectionListener() {

			@Override
			public void widgetSelected(SelectionEvent e) {

				Object sel = ((IStructuredSelection) itemsViewer.getSelection())
						.getFirstElement();
				if (sel != null && sel instanceof LineItem) {
					LineItem item = (LineItem) sel;
					items.remove(item);
					itemsViewer.setInput(items.toArray(new LineItem[0]));
				}
			}

			@Override
			public void widgetDefaultSelected(SelectionEvent e) {
			}
		});

		this._clrBtn = SWTUtils.createPushButton(btnComp, "Clear");
		this._clrBtn.addSelectionListener(new SelectionListener() {

			@Override
			public void widgetSelected(SelectionEvent e) {
				items.clear();
				itemsViewer.setInput(items.toArray(new LineItem[0]));
			}

			@Override
			public void widgetDefaultSelected(SelectionEvent e) {

			}
		});
	}

	public List<LineItem> getItems() {
		return this.items;
	}

}
